<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择用户类型" clearable size="small">
          <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel":value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="用户状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择用户类型" clearable size="small">
          <el-option v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictLabel":value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱地址" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel":value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信状态" prop="wechatState">
        <el-select v-model="queryParams.wechatState" placeholder="请选择微信状态" clearable size="small">
          <el-option v-for="dict in wechatStateOptions" :key="dict.dictValue" :label="dict.dictLabel":value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="信用代码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入信用代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录IP" prop="loginIp">
        <el-input
          v-model="queryParams.loginIp"
          placeholder="请输入登录IP"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录时间" prop="loginDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.loginDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择登录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.createDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" width="100"/>
      <el-table-column label="用户类型" align="center" prop="type" :formatter="typeFormat" width="100"/>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            :active-value="0"
            :inactive-value="1"
            @change="handleStateChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" width="100"/>
      <el-table-column label="真实姓名" align="center" prop="fullName" width="100"/>
      <el-table-column label="企业名称" align="center" prop="enterpriseName" width="200"/>
      <el-table-column label="邮箱地址" align="center" prop="email" width="200"/>
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat"/>
      <el-table-column label="手机号" align="center" prop="phone" width="200"/>
      <el-table-column label="微信状态" align="center" prop="wechatState" :formatter="wechatStateFormat"/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="信用代码" align="center" prop="creditCode" width="200"/>
      <el-table-column label="登录IP" align="center" prop="loginIp" width="200"/>
      <el-table-column label="登录时间" align="center" prop="loginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改前端-用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="passWord">
          <el-input v-model="form.passWord" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="form.type" placeholder="请选择用户类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="账号状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入用户状态" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="微信状态" prop="wechatState">
          <el-input v-model="form.wechatState" placeholder="请输入微信状态" />
        </el-form-item>

        <el-form-item label="信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入信用代码" />
        </el-form-item>
        <el-form-item label="登录时间" prop="loginDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.loginDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.createDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listUser, getUser, delUser, addUser, updateUser, exportUser, changeUserstate } from "@/api/manage/hzjyUser";

  export default {
    name: "User",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 前端-用户表格数据
        userList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          passWord: undefined,
          type: undefined,
          state: undefined,
          nickName: undefined,
          fullName: undefined,
          enterpriseName: undefined,
          email: undefined,
          sex: undefined,
          phone: undefined,
          wechatState: undefined,
          openId: undefined,
          avatar: undefined,
          creditCode: undefined,
          loginIp: undefined,
          loginDate: undefined,
          createDate: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        stateOptions:[],
        typeOptions:[],
        wechatStateOptions:[],
        sexOptions:[]
      };
    },
    created() {
      this.getList();
      this.getDicts("hzjy_user_state").then(response=>{
        this.stateOptions=response.data;
      });
      this.getDicts("hzjy_user_type").then(response=>{
        this.typeOptions=response.data;
      })
      this.getDicts("hzjy_user_wechat_state").then(response=>{
        this.wechatStateOptions=response.data;
      })
      this.getDicts("sys_user_sex").then(response=>{
        this.sexOptions=response.data;
      })
    },
    methods: {
      /** 查询前端-用户列表 */
      getList() {
        this.loading = true;
        listUser(this.queryParams).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      stateFormat(row,column){
        return this.selectDictLabel(this.stateOptions,row.state);
      },
      typeFormat(row,column){
        return this.selectDictLabel(this.typeOptions,row.type);
      },
      wechatStateFormat(row,column){
        return this.selectDictLabel(this.wechatStateOptions,row.wechatState);
      },
      sexFormat(row,column){
        return this.selectDictLabel(this.sexOptions,row.sex);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          userId: undefined,
          userName: undefined,
          passWord: undefined,
          type: undefined,
          state: undefined,
          nickName: undefined,
          fullName: undefined,
          enterpriseName: undefined,
          email: undefined,
          sex: undefined,
          phone: undefined,
          wechatState: undefined,
          openId: undefined,
          remark: undefined,
          avatar: undefined,
          creditCode: undefined,
          loginIp: undefined,
          loginDate: undefined,
          createDate: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加前端-用户";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const userId = row.userId || this.ids
        getUser(userId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改前端-用户";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.userId != undefined) {
              updateUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.userId || this.ids;
        this.$confirm('是否确认删除前端-用户编号为"' + userIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有前端-用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },

      /** 修改状态值*/
      handleStateChange(row) {
        let text=row.state === 0 ? "启用":"封禁";
        this.$confirm('确认要' + text + '"' + row.userName + '"用户吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeUserstate(row.userId, row.state);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.state = row.state === 0 ? 1 : 0;
        });
      }
    }
  };
</script>
